package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.LocalERentScopeAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@AttrHandler(LocalERentScopeAttrHandler.class)
public class LocalERentScopeLinkAttrHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    private OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String loginProvinceNbr = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        String eparchyCode = ContextUtil.getLoginInfo().getUserInfo().getPostLanId();



        //本地区间,本地区内
        if(KiteStringUtils.equalsAny(attr.getValue(), "1_0000112","1_0000111")){
            //省份(Z端)
            KiteAttr zProvinceAttr = inst.getAttr("2_0000107");
            if(Objects.nonNull(zProvinceAttr)){
                operateInstAttrService.setAttrValue(inst, zProvinceAttr.getFieldName(), loginProvinceNbr, data.getSceneInstId(), null);
            }

            //地市(Z端)
            KiteAttr zCityAttr = inst.getAttr("2_0000109");
            if (Objects.nonNull(zCityAttr)){
                LocalAttrHelper.loadCity(loginProvinceNbr, zCityAttr);
                operateInstAttrService.setAttrValue(inst, zCityAttr.getFieldName(), eparchyCode, data.getSceneInstId(), null);
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
